package edu.hawaii.duedates;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;

/**
 * Test case for the DueDates class. Tests for successful login, unsuccessful login, etc. Does not
 * test main method.
 * 
 * @author Scheller Sanchez and Tyler Wolff
 */

public class TestDueDates {

  // The user's UH ID number.
  private final String uhTestId1 = System.getenv("DUEDATES_TESTUSER1_ID");
  private final String uhTestId2 = System.getenv("DUEDATES_TESTUSER2_ID");

  // The user's last name.
  private final String lastTestName1 = System.getenv("DUEDATES_TESTUSER1_LASTNAME");
  private final String lastTestName2 = System.getenv("DUEDATES_TESTUSER2_LASTNAME");
  
  // The -uhm parameter.
  private static final String uhm = "-uhm";

  /**
   * Testing basic object creation within class.
   */
  @Test
  public void testDueDates() {
    String [] params = {uhm, "1234", "Test", "-hsl", "4444", "5678", "-sort", "duedate", 
        "-within", "1", "-console", "-email", "tyler@test.com", "lk3j34", "-wakeup", "0.8"};
    DueDates duedate = new DueDates(params);
    assertNotNull("checking object creation", duedate);
    
    // Testing that parsing works
    assertEquals("Testing -sort", "duedate", duedate.getSort().value);
    assertEquals("Testing -within", 1, duedate.getWithin().value);
    assertTrue("Testing -console", duedate.getConsole().value);
    assertEquals(0.8, duedate.getWakeup().value, 0);
    assertEquals("Testing -uhm", "1234", duedate.getUhCommandInfo()[0]);
    assertEquals("Testing -hsl", 4444, duedate.getHslParam()[0]);
    assertEquals("Testing -email", "tyler@test.com", duedate.getEmail()[0]);  
    
  }
  
  /**
   * Tests an instance with no items out. Uses a test account
   * with no books out.
   */
  @Test
  public void testNoItemsDue() {
    String [] args = {uhm, uhTestId1, lastTestName1};
    DueDates noItems = new DueDates(args);
    noItems.setItemsDue(noItems.getItems());
    assertFalse("Testing no items", noItems.haveItemsOut());
  }
  
  /**
   * Tests an instance with items out. Uses a test account
   * with two books out.
   */
  @Test
  public void testItemsDue() {
    String [] args = {uhm, uhTestId2, lastTestName2, "-sort", "duedate"};
    DueDates items = new DueDates(args);
    items.setItemsDue(items.getItems());
    assertTrue("Testing no items", items.haveItemsOut());
    assertEquals("Testing items", 2, items.getItemsDue().size());
  }
  
  /**
   * Tests the toString method in DueDates class for no Items.
   */
  @Test
  public void testToStringNoItems() {
    // creating DueDate object
    String [] args = {};
    DueDates toStringDueDates = new DueDates(args);
    List<ItemDue> allItemsDue = new ArrayList<ItemDue>();
    toStringDueDates.setItemsDue(allItemsDue);
    assertEquals("Testing no items", "You have no items currently due", 
        toStringDueDates.toString());
  }
  
  /**
   * Test the within method of DueDates.
   */
  @Test
  public void testDueDatesWithin() {
    
    String [] paramArgs = {uhm, uhTestId2, lastTestName2, "-within", "1"};
    DueDates items = new DueDates(paramArgs);
    items.setItemsDue(items.getItems());
    
    assertFalse("No items", items.haveItemsOut());
  }
  
  /**
   * Tests the toString method in DueDates class with items.
   */
  @Test
  public void testToStringItems() {

    String [] args = {};
    DueDates dDates = new DueDates(args);
    List<ItemDue> allItems = new ArrayList<ItemDue>();
    allItems.add(new ItemDue("UH Manoa", "Title                                            " +
        "      ", "book", "11-20-08"));
    dDates.setItemsDue(allItems);
    
    assertNotNull("Testing items", dDates.toString());
  }
}